package com.yupi.cli.command;

import cn.hutool.core.bean.BeanUtil;
import com.yupi.model.DataModel;
import com.yupi.generator.MainGenerator;
import freemarker.template.TemplateException;
import lombok.Data;
import picocli.CommandLine;


import java.io.IOException;
import java.util.concurrent.Callable;


/**
 * @version 1.0
 * @Author yupi
 * @Date 2024-09-24 20:11:09
 * @注释 生成代码命令
 */
@CommandLine.Command(name="generate",mixinStandardHelpOptions = true)
@Data
public class GenerateCommand implements Callable<Integer> {


        /**
         * MySQL数据库配置
         */
        static DataModel.MysqlConfig mysqlConfig = new DataModel.MysqlConfig();
        @CommandLine.Command(name = "mysqlConfig")
        @Data
        public static class MysqlConfigCommand implements Runnable {
        @CommandLine.Option(names = {"--password"}, arity = "0..1", description = "密码", interactive = true, echo = true)
        private String password = "17723670835yang";
        @CommandLine.Option(names = {"--url"}, arity = "0..1", description = "地址", interactive = true, echo = true)
        private String url = "jdbc:mysql://localhost:3306/my_db";
        @CommandLine.Option(names = {"--username"}, arity = "0..1", description = "用户名", interactive = true, echo = true)
        private String username = "root";

            @Override
            public void run() {
                mysqlConfig.password = password;
                mysqlConfig.url = url;
                mysqlConfig.username = username;
            }
        }


        /**
         * 接口文档配置
         */
        static DataModel.DocsConfig docsConfig = new DataModel.DocsConfig();
        @CommandLine.Command(name = "docsConfig")
        @Data
        public static class DocsConfigCommand implements Runnable {
        @CommandLine.Option(names = {"--description"}, arity = "0..1", description = "接口文档描述", interactive = true, echo = true)
        private String description = "springboot-init";
        @CommandLine.Option(names = {"--title"}, arity = "0..1", description = "接口文档标题", interactive = true, echo = true)
        private String title = "接口文档";
        @CommandLine.Option(names = {"--version"}, arity = "0..1", description = "接口文档版本", interactive = true, echo = true)
        private String version = "1.0";

            @Override
            public void run() {
                docsConfig.description = description;
                docsConfig.title = title;
                docsConfig.version = version;
            }
        }


    @CommandLine.Option(names = {"--needDocs"}, arity = "0..1", description = "是否开启接口文档功能", interactive = true, echo = true)
    private boolean needDocs = true;


    @CommandLine.Option(names = {"--needPost"}, arity = "0..1", description = "是否开启帖子功能", interactive = true, echo = true)
    private boolean needPost = true;


    @CommandLine.Option(names = {"--needCors"}, arity = "0..1", description = "是否开启跨域功能", interactive = true, echo = true)
    private boolean needCors = true;


    @CommandLine.Option(names = {"--basePackage"}, arity = "0..1", description = "基础包名", interactive = true, echo = true)
    private String basePackage = "com.yupi";


    public Integer call() throws Exception {
      System.out.println("输入MySQL数据库配置配置：");
      CommandLine mysqlConfigCommandLine = new CommandLine(MysqlConfigCommand.class);
      mysqlConfigCommandLine.execute("--password", "--url", "--username");
                if (needDocs) {
            System.out.println("输入接口文档配置配置：");
            CommandLine docsConfigCommandLine = new CommandLine(DocsConfigCommand.class);
            docsConfigCommandLine.execute("--description", "--title", "--version");
                }
        DataModel dataModel = new DataModel();
        BeanUtil.copyProperties(this, dataModel);
            dataModel.mysqlConfig = mysqlConfig;
            dataModel.docsConfig = docsConfig;
        MainGenerator.doGenerate(dataModel);
        return 0;
    }
}
